package org.eiahe.hr.salary.controller;

import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.core.validate.QueryGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.salary.domain.bo.SalaryAdjustmentRecordBo;
import org.eiahe.hr.salary.domain.vo.SalaryAdjustmentRecordVo;
import org.eiahe.hr.salary.service.ISalaryAdjustmentRecordService;
import org.ehe.common.excel.utils.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckPermission;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 调薪记录Controller
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/salary/adjustment/record")
public class SalaryAdjustmentRecordController extends BaseController {

    private final ISalaryAdjustmentRecordService iSalaryAdjustmentRecordService;

    /**
     * 查询调薪记录列表
     */
    @SaCheckPermission("salaryAdjustment:record:list")
    @GetMapping("/list")
    public TableDataInfo<SalaryAdjustmentRecordVo> list(SalaryAdjustmentRecordBo bo, PageQuery pageQuery) {
        return iSalaryAdjustmentRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出调薪记录列表
     */
    @SaCheckPermission("salaryAdjustment:record:export")
    @Log(title = "调薪记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SalaryAdjustmentRecordBo bo, HttpServletResponse response) {
        List<SalaryAdjustmentRecordVo> list = iSalaryAdjustmentRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "调薪记录数据", SalaryAdjustmentRecordVo.class, response);
    }

    /**
     * 获取调薪记录详细信息
     *
     * @param recordId 主键
     */
    @SaCheckPermission("salaryAdjustment:record:query")
    @GetMapping("/{recordId}")
    public R<SalaryAdjustmentRecordVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("recordId") Long recordId) {
        return R.ok(iSalaryAdjustmentRecordService.queryById(recordId));
    }

    /**
     * 新增调薪记录
     */
    @SaCheckPermission("salaryAdjustment:record:add")
    @Log(title = "调薪记录", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SalaryAdjustmentRecordBo bo) {
        return toAjax(iSalaryAdjustmentRecordService.insertByBo(bo));
    }

    /**
     * 修改调薪记录
     */
    @SaCheckPermission("salaryAdjustment:record:edit")
    @Log(title = "调薪记录", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SalaryAdjustmentRecordBo bo) {
        return toAjax(iSalaryAdjustmentRecordService.updateByBo(bo));
    }

    /**
     * 删除调薪记录
     *
     * @param recordIds 主键串
     */
    @SaCheckPermission("salaryAdjustment:record:remove")
    @Log(title = "调薪记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{recordIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] recordIds) {
        return toAjax(iSalaryAdjustmentRecordService.deleteWithValidByIds(Arrays.asList(recordIds)));
    }

    /**
     * 根据调薪申请创建调薪记录
     */
    @SaCheckPermission("salaryAdjustment:record:create")
    @Log(title = "调薪记录", businessType = BusinessType.INSERT)
    @PostMapping("/create-from-application/{applicationId}")
    public R<Void> createFromApplication(@NotNull(message = "申请ID不能为空") @PathVariable Long applicationId) {
        return toAjax(iSalaryAdjustmentRecordService.createRecordFromApplication(applicationId));
    }
}
